/**
 * This class represents a warehouse-handler
 * 
 * @author Konrad Paumann
 * @author Bernhard Denner
 * @author Maximilian Csuk
 * 
 * $Revision$ 
 * $Date$
 * 
 */

public class WarehouseHandler extends Handler {
	/**
	 * variables
	 */
	private final Warehouse warehouse;
	
	/**
	 * constructor
	 * @param warehouse the warehouse to handle
	 * @param user the user who wants to alter the warehouse
	 */
	public WarehouseHandler(Warehouse warehouse, User user, Logger logger) {
		super(user, logger);
		assert warehouse != null;
		this.warehouse = warehouse;
	}
	
	/**
	 * adds a product-group to the warehouse
	 * @param name the group-name
	 * @param tax the tax
	 * @throws UserException
	 * @throws ProductException
	 */
	public void addProductGroup(String name, int tax) throws UserException, ProductException {
		if (!getUser().canWriteWarehouse()) {
			getLogger().log(getUser().getName(), "Hinzufuegen von Produkt-Gruppe verweigert");
			throw new UserException("User nicht authorisiert");
		}
		
		try {
			warehouse.addProductGroup(name, tax);
		} catch (ProductException e) {
			throw new ProductException(e.getMessage());
		}
		
		getLogger().log(getUser().getName(), "Hinzufuegen von Produkt-Gruppe erfolgreich");
	}
	
	/**
	 * removes a product-group from the warehouse
	 * @param user the user who wants to remove a product-group
	 * @param name the group-name
	 * @throws UserException
	 * @throws ProductException
	 */
	public void removeProductGroup(String name) throws UserException, ProductException {
		if (!getUser().canWriteWarehouse()) {
			getLogger().log(getUser().getName(), "Entfernen von Produkt-Gruppe verweigert");
			throw new UserException("User nicht authorisiert");
		}
		
		try {
			warehouse.removeProductGroup(name);
		} catch (ProductException e) {
			throw new ProductException(e.getMessage());
		}
		
		getLogger().log(getUser().getName(), "Entfernen von Produkt-Gruppe erfolgreich");
	}
	
}
